package com.icart.context.storage;


import com.icart.context.HttpUserContext;
import com.icart.context.SaleUserContext;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by kim on 17/11/20.
 */
public class SessionContextStorageImpl implements  ContextStorage {

    private HttpServletRequest request;
    public  SessionContextStorageImpl(HttpServletRequest request){
        this.request= request;
    }

    /*存入session*/
    public void save(String key, HttpUserContext userContext) {
        if(userContext!=null && key!=null){
            request.getSession().setAttribute(key,userContext);
        }
    }

    /*取出session*/
    public HttpUserContext getUserContext(String key) {
        Object obj= request.getSession().getAttribute(key);
        if(obj!=null){
            return (HttpUserContext)obj;
        }
        return null;
    }

    /*销售管理的存session*/
    @Override
    public void saveSale(String key, SaleUserContext saleUserContext) {
        if(saleUserContext!=null && key!=null){
            request.getSession().setAttribute(key,saleUserContext);
        }
    }

    @Override
    public SaleUserContext getsaleUserContext(String key) {
        Object obj= request.getSession().getAttribute(key);
        if(obj!=null){
            return (SaleUserContext)obj;
        }
        return null;
    }

    /*退出登录*/
    @Override
    public void clear(String key) {
        Object obj= request.getSession().getAttribute(key);
        if(obj!=null){
            request.getSession().removeAttribute(key);
        }
    }

}
